<?php

declare(strict_types=1);

namespace App\Request\Cooperative;

use App\Constants\ErrorCode;
use App\Constants\Model\ConfigOther;
use App\Exception\BusinessException;
use Hyperf\Validation\Request\FormRequest;
use Hyperf\Validation\Rule;

class ConfigOtherSaveRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $name = $this->route('name');

        if(!in_array($name,array_keys(ConfigOther::TEMPLATES))){
            throw new BusinessException(ErrorCode::COOPERATIVE_CONFIG_OTHER_NOT_FOUND);
        }

        switch ($name){
            case ConfigOther::CONFIG_AUTO_PAYMENT_NAME:
                return [
                    'value' => [
                        'required',
                        Rule::in(ConfigOther::TEMPLATES[ConfigOther::CONFIG_AUTO_PAYMENT_NAME]['attribute_list'])
                    ]
                ];
                break;
            case ConfigOther::CONFIG_COMMISSION_PERCENT_NAME:
                return [
                    'value' => 'required|integer|between:0,50'
                ];
                break;
            case ConfigOther::CONFIG_SERVICE_PHONE_NAME:
                return [
                    'value' => 'required'
                ];
                break;
        }

    }
}
